/*************************************************************
 * 
 * CopyRight(c)2007, http://www.moorwind.com/
 * email:ezhung@gmail.com
 * for free, but a email permission would be nice
 * 
 ************************************************************/
package kono.utils
{
	//简单数组栈， simple stack
	public class ArrayedStack
	{
		//栈的大小， size of the stack
		private var _size:uint;
		
		//当前栈顶， current top item
		private var _top:uint;
		
		private var _stack:Array;
		
		public function ArrayedStack(size:uint)
		{
			_stack = new Array(_size = size);
		}
		
		public function get size():uint
		{
			return _size;
		}
		
		public function isEmpty():Boolean
		{
			return _top==0;
		}
		
		//取得当前栈顶元素， get the top item;
		public function peek():*
		{
			if(_top > 0) return _stack[_top-1];
			else return null;
		}
		
		//将给定数据进栈， push the given data into the stack
		public function push(data:*):Boolean
		{
			if(_top <= _size)
			{
				_stack[_top++] = data;;
			}
			return false;
		}
		
		//栈顶数据出栈， remove the top item in the stack
		public function pop():void
		{
			if(_top > 0) _stack[--_top] = null;
		}
		
		//重新设定栈的大小， reset the size of the stack
		public function ensureCapacity(capacity:uint):void
		{
			var a:Array = new Array(_size = capacity);
			for (var i:uint = 0; i < _stack.length; i++)
			{
				a[i] = _stack[i];
			}
			_stack = a;
		}
		
		public function toString():String
		{
			return "[ArrayedStack, size= " + _size + "]";
		}
	}
}